﻿using MediatR;
using Medical.Api.Read.Application.Command.RecyclingCleaning;
using Medical.Api.Read.Application.Command.RecyclingCleaning.Disinfects;
using Medical.Api.Read.Dto.RecyclingCleaning;
using Medical.Api.Read.Dto.RecyclingCleaning.disinfects;
using Medical.Domain.DepartmentInfo;
using Medical.Domain.RecoveryCleaning;
using Medical.ErrorCode;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Medical.Api.Read.Controllers
{
    /// <summary>
    /// 回收清理管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RecyclingCleaningController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<RecyclingCleaningController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public RecyclingCleaningController(ILogger<RecyclingCleaningController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取待回收物品信息
        /// </summary>
        /// <returns>待回收物品列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<RecoveredDtos>>> GetItemInfo()
        {
            try
            {
                logger.LogInformation("获取待回收物品信息");
                var command = new GetItemInfoCommand();
                var result = await mediator.Send(command);
                return result;
            }
            catch (System.Exception ex)
            {
                logger.LogError(ex, "获取待回收物品信息时发生异常");
                return new ApiResult<List<RecoveredDtos>>
                {
                    Code = ApiEnums.Error,
                    Msg = "获取待回收物品信息失败：" + ex.Message,
                    Data = null
                };
            }
        }
        /// <summary>
        /// 获取已回收物品信息
        /// </summary>
        /// <returns>已回收物品列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<RecoveredFinshDto>>> GetRecoveredFinsh()
        {
            try
            {
                logger.LogInformation("获取已回收物品信息");
                var command = new GetRecoveredListCommand();
                var result = await mediator.Send(command);
                return result;
            }
            catch (System.Exception ex)
            {
                logger.LogError(ex, "获取已回收物品信息时发生异常");
                return new ApiResult<List<RecoveredFinshDto>>
                {
                    Code = ApiEnums.Error,
                    Msg = "获取已回收物品信息失败：" + ex.Message,
                    Data = null
                };
            }
        }

        /// <summary>
        /// 获取清洗登记信息列表
        /// </summary>
        /// <returns>清洗登记信息列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<RegistrationCleaningDtos>>> GetRegistrationCleaningList()
        {
            try
            {
                logger.LogInformation("获取清洗登记信息列表");
                var command = new GetRegistrationCleaningCommand();
                var result = await mediator.Send(command);
                return result;
            }
            catch (System.Exception ex)
            {
                logger.LogError(ex, "获取清洗登记信息列表时发生异常");
                return new ApiResult<List<RegistrationCleaningDtos>>
                {
                    Code = ApiEnums.Error,
                    Msg = "获取清洗登记信息列表失败：" + ex.Message,
                    Data = null
                };
            }
        }

        /// <summary>
        /// 获取清洗完成列表信息
        /// </summary>
        /// <param name="finishTime">完成时间筛选条件</param>
        /// <returns>清洗完成信息列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<OffTheStocksDtos>>> GetOffTheStocksList([FromQuery] string finishTime = null)
        {
            try
            {
                logger.LogInformation("获取清洗完成列表信息");
                var command = new GetOffTheStocksCommand { FinishTime = finishTime };
                var result = await mediator.Send(command);
                return result;
            }
            catch (System.Exception ex)
            {
                logger.LogError(ex, "获取清洗完成列表信息时发生异常");
                return new ApiResult<List<OffTheStocksDtos>>
                {
                    Code = ApiEnums.Error,
                    Msg = "获取清洗完成列表信息失败：" + ex.Message,
                    Data = null
                };
            }
        }

        /// <summary>
        /// 获取物品信息列表，可根据物品条码过滤
        /// </summary>
        /// <param name="itemCode">物品条码（可选）</param>
        /// <returns>物品信息列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<ItemtableDtos>>> GetItemTableInfo([FromQuery]GetitemTableInfoCommand command)
        {
            try
            {
                logger.LogInformation("获取物品信息列表，条码过滤：{ItemCode}", command.ItemCode);
 
                var result = await mediator.Send(command);
                return result;
            }
            catch (System.Exception ex)
            {
                logger.LogError(ex, "获取物品信息列表时发生异常");
                return new ApiResult<List<ItemtableDtos>>
                {
                    Code = ApiEnums.Error,
                    Msg = "获取物品信息列表失败：" + ex.Message,
                    Data = null
                };
            }
        }

        /// <summary>
        /// 获取已清洗物品信息列表
        /// </summary>
        /// <returns>已清洗物品信息列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<CleanedDtos>>> GetCleanedItemList()
        {
            try
            {
                logger.LogInformation("获取已清洗物品信息列表");
                var command = new GetCleanedItemCommand();
                var result = await mediator.Send(command);
                return result;
            }
            catch (System.Exception ex)
            {
                logger.LogError(ex, "获取已清洗物品信息列表时发生异常");
                return new ApiResult<List<CleanedDtos>>
                {
                    Code = ApiEnums.Error,
                    Msg = "获取已清洗物品信息列表失败：" + ex.Message,
                    Data = null
                };
            }
        }

        /// <summary>
        /// 获取待消毒列表信息
        /// </summary>
        /// <returns>待消毒列表信息</returns>
        [HttpGet]
        public async Task<ApiResult<List<DisinfectionRegistration>>> GetDisinfectedList()
        {
            try
            {
                logger.LogInformation("获取待消毒列表信息");
                var command = new GetDisinfectedListCommand();
                var result = await mediator.Send(command);
                return result;
            }
            catch (System.Exception ex)
            {
                logger.LogError(ex, "获取待消毒列表信息时发生异常");
                return new ApiResult<List<DisinfectionRegistration>>
                {
                    Code = ApiEnums.Error,
                    Msg = "获取待消毒列表信息失败：" + ex.Message,
                    Data = null
                };
            }
        }

        /// <summary>
        /// 获取已完成消毒列表信息
        /// </summary>
        /// <returns>已完成消毒列表信息</returns>
        [HttpGet]
        public async Task<ApiResult<List<DisinfectionRegistration>>> GetAccomplishDisinfectedList()
        {
            try
            {
                logger.LogInformation("获取已完成消毒列表信息");
                var command = new GetaccomplishDisinfectedCommand();
                var result = await mediator.Send(command);
                return result;
            }
            catch (System.Exception ex)
            {
                logger.LogError(ex, "获取已完成消毒列表信息时发生异常");
                return new ApiResult<List<DisinfectionRegistration>>
                {
                    Code = ApiEnums.Error,
                    Msg = "获取已完成消毒列表信息失败：" + ex.Message,
                    Data = null
                };
            }
        }

       
    }
   
}
